package com.youlai.boot.modules.content.controller;

import com.youlai.boot.modules.content.service.LzArticleService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.modules.content.model.form.LzArticleForm;
import com.youlai.boot.modules.content.model.query.LzArticleQuery;
import com.youlai.boot.modules.content.model.vo.LzArticleVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

import java.util.List;

/**
 * 文章前端控制层
 *
 * @author lz0816
 * @since 2025-02-25 16:26
 */
@Tag(name = "文章接口")
@RestController
@RequestMapping("/api/v1/lzBlogArticles")
@RequiredArgsConstructor
public class LzBlogArticleController {

    private final LzArticleService lzArticleService;

    @Operation(summary = "文章分页列表")
    @GetMapping("/page")
    public PageResult<LzArticleVO> getLzArticlePage(LzArticleQuery queryParams) {
        IPage<LzArticleVO> result = lzArticleService.getLzArticlePage(queryParams);
        return PageResult.success(result);
    }


    @Operation(summary = "获取文章表单数据")
    @GetMapping("/{id}/form")
    public Result<LzArticleForm> getLzArticleForm(
            @Parameter(description = "文章ID") @PathVariable Long id
    ) {
        LzArticleForm formData = lzArticleService.getLzArticleFormData(id);
        return Result.success(formData);
    }

    // 更新文章浏览量
    @Operation(summary = "更新文章浏览量")
    @PutMapping("/{id}/view_count")
    public Result<Void> updateArticleViewCount(
            @Parameter(description = "文章ID") @PathVariable Long id
    ) {
        lzArticleService.increaseViewCount(id);
        return Result.success();
    }

    // 获取热门文章
    @Operation(summary = "获取热门文章")
    @GetMapping("/hot")
    public Result<List<LzArticleVO>> getHotArticles() {
        List<LzArticleVO> lzArticleVOs = lzArticleService.getHotArticles();
        return Result.success(lzArticleVOs);
    }
}
